package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /*
     *  登录
     * */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1、将页面提交的密码password进行md5加密处理  DigestUtils.md5DigestAsHex
        String password = employee.getPassword();
//        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(wrapper);
        //3、如果没有查询到则返回登录失败结果
        if (one == null) {
            log.info("用户名或密码有误");
            return R.error("用户名或密码错误");
        }
        //4、密码比对，如果不一致则返回登录失败结果
        if (password.equals(one.getPassword())) {
            log.info("用户名或密码有误");
            return R.error("用户名或密码错误");
        }
        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus() == 0) {
            log.info("该账号处于禁用状态");
            return R.error("该账号处于禁用状态");
        }
        //6、登录成功，将员工id存入Session, 并返回登录成功结果
        long id = Thread.currentThread().getId();
        request.getSession().setAttribute("employee",id);
        log.info("线程id为：{}",id);
        return R.success(one);
    }

    /*
     *  退出账户
     *
     *  */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        log.info("退出");
        //销毁Session方法
        request.getSession().invalidate();
        return R.success();
    }

    //新增
    @PostMapping()
    public R save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工,{}", employee.toString());
        //初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //赋值当前登录用户
        Long employeeId = (Long) request.getSession().getAttribute("employee");
        employee.setStatus(1);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(employeeId);
//        employee.setUpdateUser(employeeId);
        employeeService.save(employee);
        return R.success();
    }

    /*
     *
     *   分页查询
     * */
    @GetMapping("/page")
    public R<Page<Employee>> page(@RequestParam(required = false, defaultValue = "1") int page,
                                  @RequestParam(required = false, defaultValue = "10") int pageSize, String name) {
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }


    /*
    *
    *   修改员工信息
    * */

    @PutMapping
    public R update(HttpServletRequest request, @RequestBody Employee employee){
        log.info("执行修改员工操作, employee = {}", employee.toString());
        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        //2. 执行更新
        employeeService.updateById(employee);
        return R.success();
    }

    /*
    * 根据id查询用户
    *
    *
    * */

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id){
        log.info("根据id查询用户信息，id={}",id);
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return  R.success(employee);
        }
        return R.error("查询失败");
    }





    }


